package sequencing;

import anim.AnimFragmentStatus;
import anim.Matrix3D;

public class AnimController
{
    public static Matrix3D calculateTransformForOneBone(DaeInfoManager infoManager, int frame, String boneName)
    {
        BoneInfo boneInfo = infoManager.boneInfoMap.get(boneName);
        if (boneInfo.animFragments == null)
        {
            //此骨骼上没有动画数据,
            // 本身不为根节点返回其父节点的值
            if (boneInfo.object.parent != null)
            {
                return calculateTransformForOneBone(infoManager, frame, boneInfo.object.parent.name);
            }
            else
            {
                return Matrix3D.getOneMatrix();
            }

        }
        //已有直接返回
        if (boneInfo.animFragments.nodeToRootAnimStatusMap.containsKey(frame))
        {
            AnimFragmentStatus fragmentStatus = boneInfo.animFragments.nodeToRootAnimStatusMap.get(frame);
            if (fragmentStatus.status == null)
            {
                fragmentStatus.status = new Matrix3D();
            }
            return boneInfo.animFragments.nodeToRootAnimStatusMap.get(frame).status;
        }
        //父节点为空，表示为根节点，取相对父节点数据复制一份
        if (boneInfo.object.parent == null)
        {
            AnimFragmentStatus fragmentStatus = boneInfo.animFragments.nodeToParentAnimStatusMap.get(frame).clone();
            boneInfo.animFragments.nodeToRootAnimStatusMap.put(frame, fragmentStatus);
            return fragmentStatus.status;
        }
        else
        {
            String parentBoneName = boneInfo.object.parent.name;
            Matrix3D parentToRoot = calculateTransformForOneBone(infoManager, frame, parentBoneName);
            Matrix3D boneToParent = boneInfo.animFragments.nodeToParentAnimStatusMap.get(frame).status;
            Matrix3D boneToRoot = boneToParent.multi(parentToRoot);
            AnimFragmentStatus boneToRootAnimStatus = boneInfo.animFragments.nodeToParentAnimStatusMap.get(frame).clone();
            boneToRootAnimStatus.status = boneToRoot;
            boneInfo.animFragments.nodeToRootAnimStatusMap.put(frame, boneToRootAnimStatus);
            return boneToRoot;
        }
    }
}
